package com.ibeeking.found.msg.rest.service.impl;

import com.ibeeking.found.common.enums.mp.WxMpSourceTypeEnum;
import com.ibeeking.found.msg.rest.service.IEventMsgService;
import com.ibeeking.found.msg.rest.service.IWxMpSourceService;
import com.ibeeking.found.msg.rest.service.util.ReplyMessageUtils;
import com.ibeeking.found.msg.service.common.bo.MessageInfoBO;
import com.ibeeking.found.msg.service.common.bo.WxMpSourceBO;
import com.ibeeking.found.msg.service.common.query.CallBackQuery;
import com.ibeeking.found.msg.service.common.vo.WxMpSourceVO;

import me.chanjar.weixin.common.api.WxConsts;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName EventMsgServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-02-18 14:23
 **/
@Service
public class EventMsgServiceImpl implements IEventMsgService {

    @Resource
    private IWxMpSourceService mpSourceServiceImpl;

    @Override
    public WxMpXmlOutMessage callBackMsg(WxMpXmlMessage wxMessage, CallBackQuery callBackQuery) {
        List<MessageInfoBO> messageInfos = new ArrayList<>();
        if (null == callBackQuery.getSourceType() || WxMpSourceTypeEnum.TEXT.getName().equals(callBackQuery.getSourceType().getName())) {
            //文本
            MessageInfoBO messageInfo = new MessageInfoBO();
            messageInfo.setMsgType(WxConsts.XmlMsgType.TEXT);
            messageInfo.setContent(callBackQuery.getUnionKey());
            messageInfos.add(messageInfo);
        } else if (WxMpSourceTypeEnum.PIC.getName().equals(callBackQuery.getSourceType().getName())) {
            //图片
            MessageInfoBO messageInfo = new MessageInfoBO();
            messageInfo.setMsgType(WxConsts.XmlMsgType.IMAGE);
            messageInfo.setMediaId(mpSourceServiceImpl.uploadMediaToWx(Long.valueOf(callBackQuery.getUnionKey())));
            messageInfos.add(messageInfo);
        } else if (WxMpSourceTypeEnum.VOICE.getName().equals(callBackQuery.getSourceType().getName())) {
            //语音
            MessageInfoBO messageInfo = new MessageInfoBO();
            messageInfo.setMsgType(WxConsts.XmlMsgType.VOICE);
            messageInfo.setMediaId(mpSourceServiceImpl.uploadMediaToWx(Long.valueOf(callBackQuery.getUnionKey())));
            messageInfos.add(messageInfo);
        } else if (WxMpSourceTypeEnum.VIDEO.getName().equals(callBackQuery.getSourceType().getName())) {
            //视频
            MessageInfoBO messageInfo = new MessageInfoBO();
            messageInfo.setMsgType(WxConsts.XmlMsgType.VIDEO);
            String mediaId = mpSourceServiceImpl.uploadMediaToWx(Long.valueOf(callBackQuery.getUnionKey()));
            WxMpSourceBO wxMpSourceBO = mpSourceServiceImpl.querySourceById(Long.valueOf(callBackQuery.getUnionKey()));
            if (null != wxMpSourceBO) {
                messageInfo.setMediaId(mediaId);
                messageInfo.setTitle(wxMpSourceBO.getName());
                messageInfo.setDescription(wxMpSourceBO.getRemark());
            }
            messageInfos.add(messageInfo);
        } else if (WxMpSourceTypeEnum.MUSIC.getName().equals(callBackQuery.getSourceType().getName())) {
            //音乐
            MessageInfoBO messageInfo = new MessageInfoBO();
            messageInfo.setMsgType(WxConsts.XmlMsgType.MUSIC);
            messageInfo.setMediaId(mpSourceServiceImpl.uploadMediaToWx(Long.valueOf(callBackQuery.getUnionKey())));
            messageInfos.add(messageInfo);
        } else if (WxMpSourceTypeEnum.IMAGE_TEXT.getName().equals(callBackQuery.getSourceType().getName())) {
            //图文
            MessageInfoBO messageInfo = new MessageInfoBO();
            messageInfo.setMsgType(WxConsts.XmlMsgType.NEWS);
            List<WxMpXmlOutNewsMessage.Item> articles = new ArrayList<>();
            WxMpSourceVO view = mpSourceServiceImpl.view(Long.valueOf(callBackQuery.getUnionKey()));
            view.getSourceDetail().stream().forEach(mpSourceNewsVO -> {
                WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
                item.setTitle(mpSourceNewsVO.getTitle());
                item.setUrl(mpSourceNewsVO.getContentSourceUrl());
                item.setDescription(mpSourceNewsVO.getDigest());
                item.setPicUrl(mpSourceNewsVO.getThumbMediaUrl());
                articles.add(item);
            });
            messageInfo.setArticles(articles);
            messageInfos.add(messageInfo);
        } else {
            MessageInfoBO messageInfo = new MessageInfoBO();
            messageInfo.setMsgType(WxConsts.XmlMsgType.TEXT);
            messageInfo.setContent("暂时找不到您输入的问题");
            messageInfos.add(messageInfo);
        }
        return ReplyMessageUtils.replyMessage(wxMessage, messageInfos);
    }

}
